(function () {
    "use strict";

    angular.module('core').controller('reportOperacaoVV010103Controller', controller);
    controller.$inject = ['$scope', '$alert', 'toolsService', 'vendaVarejoService'];

    function controller($scope, $alert, toolsService, vendaVarejoService) {
        $scope.initializeLayout('Análise de Pagamentos Vendas de Varejo');

        //[Inicializa objetos]
        $scope.state = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                args: null,
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            $scope.clearFilter();
            $scope.refresh();
        };

        $scope.getAll = function () {
            $scope.state.filtro.args.skip = 0;
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            vendaVarejoService
                .getAllPagamento($scope.state.filtro.args)
                .then(
                function (response) {
                    if (response.Status < 0) {
                        $alert({ text: response.Message, title: $scope.Title });
                    } else {
                        angular.copy(angular.fromJson(response.Data), $scope.state.data);

                        /* Início Pivot Table */
                        var dateFormat = $.pivotUtilities.derivers.dateFormat;
                        var sortAs = $.pivotUtilities.sortAs;
                        var tpl = $.pivotUtilities.aggregatorTemplates;
                        var fmt = $.pivotUtilities.numberFormat({ suffix: " °C" });

                        $("#pivot-table").pivotUI(toolsService.flattenArray(response.Data), {
                            hiddenAttributes: [],
                            derivedAttributes: {
                                "VendaVarejo.Emissao.Ano": dateFormat("VendaVarejo.Emissao", "%y", true),
                                "VendaVarejo.Emissao.Mes": dateFormat("VendaVarejo.Emissao", "%n", true),
                                "VendaVarejo.Emissao.Dia": dateFormat("VendaVarejo.Emissao", "%d", true),
                                "VendaVarejo.Emissao.DiaSemana": dateFormat("VendaVarejo.Emissao", "%w", true),
                            },
                            sorters: {
                                "VendaVarejo.Emissao.Mes": sortAs(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]),
                                "VendaVarejo.Emissao.DiaSemana": sortAs(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
                            },
                            rows: ["VendaVarejo.Filial.Empresa.Nome", "Tipo"],
                            cols: ["VendaVarejo.Emissao.Ano", "VendaVarejo.Emissao.Mes"],
                            vals: ["Valor"],
                            aggregatorName: "Sum",
                            rendererName: "Heatmap",
                            renderers: $.extend(
                                $.pivotUtilities.renderers,
                                $.pivotUtilities.c3_renderers,
                                $.pivotUtilities.d3_renderers
                            ),
                        });

                        $scope.tools.closeColunasDinamicas();

                        /* Fim Pivot Table */

                    }
                    $scope.state.carregando = false;
                    $scope.state.vazio = ($scope.state.data == 0);
                },
                function (_error) {
                    $scope.state.carregando = false;
                    $scope.tools.toastr.error('Não foi possível conectar com o(s) servidor(es)');
                });
        };
        $scope.refresh = function () {
            $scope.getAll();
        };

        $scope.clearFilter = function () {
            $scope.state.filtro.args = {
                chaves: '',
                filtro: {
                    VendaVarejo: {
                        Status: 1
                    }
                },
                filtroDe:
                {
                    VendaVarejo: {
                        Emissao: $scope.tools.getHoje()
                    }
                },
                filtroAte: null,
                sortExpression: '',
                skip: 0,
                take: 999999
            };
        };
        $scope.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
        };

        /*Inicializa funções*/
        $scope.initialize();

    }

})();